# pyinstaller -F main.py -n CAD打散工具V1.05

from pyautocad import Autocad

acad = Autocad(create_if_not_exists = False)
# acad.prompt("Hello! AutoCAD from pyautocad.")
print(acad.doc.Name)

def start():
    """用于判断是否需要继续循环打散下去"""
    global block_list
    block_list = []
    for obj in acad.iter_objects("AcDbBlockReference"):
        block_list.append(obj)

def cadExplode():
    global block_list
    block_list = [] # 用于判断是否需要继续循环打散下去
    for obj in acad.iter_objects("AcDbBlockReference"):
        block_list.append(obj)
        # 以下获取块的属性信息
        # 如果想获取某一特定块的属性信息可以用ObjectID识别特定块
        name = obj.ObjectName
        id = obj.ObjectID
        print(name, id)
        try:
            obj.Explode()  # 炸开块
        except:
            obj.Delete()   # 删除重复

def run():
    start()
    i = 0
    while block_list:
        print(block_list)
        cadExplode()
        print(i)
        i += 1
    input("完成！按回车键继续下一次打散")

if __name__ == '__main__':
    while True:
        run()

